package com.bjb.vr.common.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.OptCusLogo;
import com.bjb.vr.common.dto.ResDevice;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.mapper.OptCusLogoMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptCusLogoVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
* @author admin
* @description 针对表【opt_cus_logo】的数据库操作Service
* @createDate 2022-09-19 09:30:40
*/
@Slf4j
@Service
public class OptCusLogoService extends ServiceImpl<OptCusLogoMapper, OptCusLogo>{

	@Autowired
	FileOperationService fileOperationService;

	@Autowired
	OptCusLogoMapper optCusLogoMapper;

	public String addWebCusLogo(@RequestParam("tenantId") String tenantId, @RequestParam("webLogoFile") MultipartFile webLogoFile){
		Assert.notNull(tenantId,"租户id不能为空");
		Assert.notNull(webLogoFile,"图片文件不能为空");
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		String webLogoFilePath = "";
		try {
			LambdaQueryWrapper<OptCusLogo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(OptCusLogo::getTenantId, tenantId);
			OptCusLogo _optCusLogo = this.getOne(lambdaQueryWrapper);
			webLogoFilePath = fileOperationService.uploadFile(webLogoFile);
			if (_optCusLogo != null) {
				_optCusLogo.setWebLogoPath(webLogoFile.getName());
				if(this.updateById(_optCusLogo)){
					return webLogoFilePath;
				}

			}else{
				OptCusLogo optCusLogo = new OptCusLogo();
				umsAccountVo.setTempTenantId(tenantId);
				optCusLogo.setWebLogoPath(webLogoFile.getName());
				if(this.save(optCusLogo)){
					return webLogoFilePath;
				}
			}

		}catch (Exception ex){
			throw new CommonException(ex.getMessage());
		}finally {
			umsAccountVo.setTempTenantId("");
			ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
		}
		return webLogoFilePath;
	}

	public String addDevCusLogo(@RequestParam("tenantId") String tenantId, @RequestParam("devLogoFile") MultipartFile devLogoFile ){
		Assert.notNull(tenantId,"租户id不能为空");
		Assert.notNull(devLogoFile,"图片文件不能为空");
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		String devLogoFilePath = "";
		try {
			LambdaQueryWrapper<OptCusLogo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(OptCusLogo::getTenantId, tenantId);
			OptCusLogo _optCusLogo = this.getOne(lambdaQueryWrapper);

			devLogoFilePath = fileOperationService.uploadFile(devLogoFile);
			if (_optCusLogo != null) {
				_optCusLogo.setDevLogoPath(devLogoFile.getName());
				if(this.updateById(_optCusLogo)){
					return devLogoFilePath;
				}
			}else{
				umsAccountVo.setTempTenantId(tenantId);
				OptCusLogo optCusLogo = new OptCusLogo();
				optCusLogo.setDevLogoPath(devLogoFile.getName());
				if(this.save(optCusLogo)){
					return devLogoFilePath;
				}
			}

		}catch (Exception ex){
			throw new CommonException(ex.getMessage());
		}finally {
			umsAccountVo.setTempTenantId("");
			ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
		}
		return devLogoFilePath;
	}

	public OptCusLogoVo getDevLogoPath (String udid){
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		MPJLambdaWrapper<OptCusLogo> mpjLambdaWrapper = new MPJLambdaWrapper();
		mpjLambdaWrapper.select(OptCusLogo::getDevLogoPath)
						.innerJoin(ResDevice.class,on->on.eq(ResDevice::getUdid,udid).eq(ResDevice::getTenantId,OptCusLogo::getTenantId));

		OptCusLogo optCusLogo = optCusLogoMapper.selectJoinOne(OptCusLogo.class,mpjLambdaWrapper);
		OptCusLogoVo optCusLogoVo = new OptCusLogoVo();

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);

		if(optCusLogo!=null) {
			if(StringUtils.isNotBlank(optCusLogo.getDevLogoPath())) {
				optCusLogoVo.setDevLogoPath(fileOperationService.downloadFile(optCusLogo.getDevLogoPath()));
			}
		}
		return optCusLogoVo;
	}

}
